package ac.neec.mio.ui.activity;

import ac.neec.mio.R;
import ac.neec.mio.profile.ProfilePreferenceSetting;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

public class ProfileActivity extends Activity implements View.OnClickListener{

	private static final String[] SEX_ITEM = new String[] { "MAN", "WOMAN" };

	private static final int AGE_MIN = 10;
	private static final int AGE_MAX = 100;
	private static final int HEIGHT_MIN = 100;
	private static final int HEIGHT_MAX = 200;
	private static final int WEIGHT_MIN = 20;
	private static final int WEIGHT_MAX = 120;
	private static final int RESTING_MEASUREMENT_MIN = 30;
	public static final int RESTING_MEASUREMENT_MAX = 100;
	public static final int AGE_DEFAULT_PARAMETER = 20;
	public static final int HEIGHT_DEFAULT_PARAMETER = 170 - HEIGHT_MIN;
	public static final int WEIGHT_DEFAULT_PARAMETER = 60 - WEIGHT_MIN;
	public static final int RESTING_HEATE_DEFAULT_PARAMETER =
			(RESTING_MEASUREMENT_MAX - RESTING_MEASUREMENT_MIN) / 2;

	private Button saveButton;
	private Button measurementButton;
	private Spinner sexSpinner;
	private SeekBar ageSeekBar;
	private SeekBar heightSeekBar;
	private SeekBar weightSeekBar;
	private SeekBar restingHeartSeekBar;
	private TextView ageText;
	private TextView heightText;
	private TextView weightText;
	private TextView restingHeartText;
	private ImageButton ageUpButton;
	private ImageButton ageDownButton;
	private ImageButton heightUpButton;
	private ImageButton heightDownButton;
	private ImageButton weightUpButton;
	private ImageButton weightDownButton;
	private ImageButton restingMeasurementUpButton;
	private ImageButton restingMeasurementDownButton;
	private ProfilePreferenceSetting setting;
	private int age;
	private int height;
	private int weight;
	private int restingHeart;
	private int rate;

	private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			if (seekBar.getId() == ageSeekBar.getId()) {
				age = progress;
				ageText.setText(String.valueOf(progress));
			} else if (seekBar.getId() == heightSeekBar.getId()) {
				height = progress;
				heightText.setText(String.valueOf(progress + HEIGHT_MIN));
			} else if (seekBar.getId() == weightSeekBar.getId()) {
				weight = progress;
				weightText.setText(String.valueOf(progress + WEIGHT_MIN));
			} else if (seekBar.getId() == restingHeartSeekBar.getId()) {
				restingHeart = progress;
				restingHeartText.setText(String.valueOf(progress + RESTING_MEASUREMENT_MIN));
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {

		}
	};

	@Override
	protected void onRestart(){
		super.onRestart();
		Log.e("", "reStart");
		setParameterSeekBar();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.e("", "profileActivityOnCreate");
		super.onCreate(savedInstanceState);
		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.activity_profile);

		setting = new ProfilePreferenceSetting(getApplicationContext());

		initFindViews();
		setLintener();
		setSeekBer();
		setParameterSeekBar();
	}

	private void setSeekBer() {
		ageSeekBar.setMax(AGE_MAX - AGE_MIN);
		heightSeekBar.setMax(HEIGHT_MAX - HEIGHT_MIN);
		weightSeekBar.setMax(WEIGHT_MAX - WEIGHT_MIN);
		restingHeartSeekBar.setMax(RESTING_MEASUREMENT_MAX - RESTING_MEASUREMENT_MIN);


		}

	private void setParameterSeekBar(){
		ageSeekBar.setProgress(setting.getAge());
		heightSeekBar.setProgress(setting.getHeight()-HEIGHT_MIN);
		weightSeekBar.setProgress(setting.getWeight()-WEIGHT_MIN);
		restingHeartSeekBar.setProgress(setting.getRestingHeart()-RESTING_MEASUREMENT_MIN);

	}

	private void setSexSpinnerItem() {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapter.addAll(SEX_ITEM);
		sexSpinner.setAdapter(adapter);
	}

	private void setLintener() {
		ageSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
		heightSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
		weightSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
		restingHeartSeekBar.setOnSeekBarChangeListener(mOnSeekBarChangeListener);

		saveButton.setOnClickListener(this);
		measurementButton.setOnClickListener(this);
		ageUpButton.setOnClickListener(this);
		ageDownButton.setOnClickListener(this);
		heightUpButton.setOnClickListener(this);
		heightDownButton.setOnClickListener(this);
		weightUpButton.setOnClickListener(this);
		weightDownButton.setOnClickListener(this);
		restingMeasurementUpButton.setOnClickListener(this);
		restingMeasurementDownButton.setOnClickListener(this);
	}

	private int setTextToInteger(TextView text) {
		return Integer.valueOf(String.valueOf(text.getText())).intValue();
	}

	private void intentResting() {
		Intent intent = new Intent(getApplicationContext(), RestingMeasurementActivity.class);
		startActivity(intent);
	}

	private void intentTest() {
		Intent intent = new Intent(getApplicationContext(), TestActivity.class);
		startActivity(intent);
	}

	private void initFindViews() {
		saveButton = (Button) findViewById(R.id.btn_save);
		measurementButton = (Button) findViewById(R.id.btn_measurement_start);
		ageSeekBar = (SeekBar) findViewById(R.id.seek_age);
		heightSeekBar = (SeekBar) findViewById(R.id.seek_height);
		weightSeekBar = (SeekBar) findViewById(R.id.seek_weight);
		restingHeartSeekBar = (SeekBar) findViewById(R.id.seek_resting_heart);
		ageText = (TextView) findViewById(R.id.edit_text_age);
		heightText = (TextView) findViewById(R.id.edit_text_height);
		weightText = (TextView) findViewById(R.id.edit_text_weight);
		restingHeartText = (TextView) findViewById(R.id.edit_text_resting_hearte);
		sexSpinner = (Spinner) findViewById(R.id.spinner_sex);
		ageUpButton = (ImageButton) findViewById(R.id.btn_age_up);
		ageDownButton = (ImageButton) findViewById(R.id.btn_age_down);
		heightUpButton = (ImageButton) findViewById(R.id.btn_height_up);
		heightDownButton = (ImageButton) findViewById(R.id.btn_height_down);
		weightUpButton = (ImageButton) findViewById(R.id.btn_weight_up);
		weightDownButton = (ImageButton) findViewById(R.id.btn_weight_down);
		restingMeasurementUpButton = (ImageButton) findViewById(R.id.btn_resting_measurement_up);
		restingMeasurementDownButton = (ImageButton) findViewById(R.id.btn_resting_measurement_down);

		setSexSpinnerItem();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {

		if (v == saveButton) {
			setting.saveProfileData(setTextToInteger(ageText),
					setTextToInteger(heightText),
					setTextToInteger(weightText),
					String.valueOf(sexSpinner.getSelectedItem()),
					setTextToInteger(restingHeartText));
			Log.e("", "age = "+(setTextToInteger(ageText)));
			Log.e("", "height = "+(setTextToInteger(heightText)));
			Log.e("", "weight = "+(setTextToInteger(weightText)));
			Log.e("", "resting = "+(setTextToInteger(restingHeartText)));

				finish();
		}
		else if (v == measurementButton) {
			intentResting();
		}
		else if (v == ageUpButton) {
			if (ageSeekBar.getProgress() + 1 < AGE_MAX - AGE_MIN) {
				ageText.setText(String.valueOf(Integer.valueOf(String.valueOf(ageText.getText())) + 1));
				ageSeekBar.setProgress(ageSeekBar.getProgress() + 1);
			}
		}
		else if (v == ageDownButton) {
			if (ageSeekBar.getProgress() - 1 > 0) {
				ageText.setText(String.valueOf(Integer.valueOf(String.valueOf(ageText.getText())) - 1));
				ageSeekBar.setProgress(ageSeekBar.getProgress() - 1);
			}
		}
		else if (v == heightUpButton) {
			if (heightSeekBar.getProgress() + 1 < HEIGHT_MAX - HEIGHT_MIN) {
				heightText.setText(String.valueOf(Integer.valueOf(String.valueOf(heightText.getText())) + 1));
				heightSeekBar.setProgress(heightSeekBar.getProgress() + 1);
			}
		}
		else if (v == heightDownButton) {
			if (heightSeekBar.getProgress() - 1 > 0) {
				heightText.setText(String.valueOf(Integer.valueOf(String.valueOf(heightText.getText())) - 1));
				heightSeekBar.setProgress(heightSeekBar.getProgress() - 1);
			}
		}
		else if (v == weightUpButton) {
			if (weightSeekBar.getProgress() + 1 < WEIGHT_MAX - WEIGHT_MIN) {
				weightText.setText(String.valueOf(Integer.valueOf(String.valueOf(weightText.getText())) + 1));
				weightSeekBar.setProgress(weightSeekBar.getProgress() + 1);
			}
		}
		else if (v == weightDownButton) {
			if (weightSeekBar.getProgress() - 1 > 0) {
				weightText.setText(String.valueOf(Integer.valueOf(
						String.valueOf(weightText.getText())) - 1));
				weightSeekBar.setProgress(weightSeekBar.getProgress() - 1);
			}
		}
		else if (v == restingMeasurementUpButton) {
			if (restingHeartSeekBar.getProgress() + 1 < RESTING_MEASUREMENT_MAX
					- RESTING_MEASUREMENT_MIN) {
				restingHeartText.setText(String.valueOf(Integer.valueOf(
						String.valueOf(restingHeartText.getText())) + 1));
				restingHeartSeekBar.setProgress(restingHeartSeekBar.getProgress() + 1);
			}
		}
		else if (v == restingMeasurementDownButton) {
			if (restingHeartSeekBar.getProgress() - 1 > 0) {
				restingHeartText.setText(String.valueOf(Integer.valueOf(
						String.valueOf(restingHeartText.getText())) - 1));
				restingHeartSeekBar.setProgress(restingHeartSeekBar.getProgress() - 1);
			}
		}
	}

}
